<?php
declare(strict_types=1);

namespace App\Model\Data;


use App\Model\Dao\IAbstract;
use Swoft\Db\Query\Builder;
use Swoft\Db\Eloquent\Model;
use Exception;
use Swoft\Log\Helper\Log;
use Swoft\Redis\Pool;
/**
 * 数据抽象类
 * Class AbstractData
 * @package App\Model\Data
 */
abstract class AbstractData implements IAbstract
{
    
    
    
    /**
     */
    protected function getParseResultByAdd(): array
    {
        return [];
    }
    
    /**
     * @var Builder
     */
    protected Model $queryBuild;
    
  
    public function add(array $values): int
    {
        try {
            
            $id = $this->getModel()->insertGetId(array_merge($values, $this->getParseResultByAdd()));
            
            vdump($id);
            
            return (int)$id;
        } catch (Exception $e) {
            Log::error($e->getMessage(), [__CLASS__, __METHOD__, __LINE__]);
            return 0;
        }
        
    }

    public function addAll(array $data): bool
    {
        return $this->getModel()->insert($data);
    }

    public function save(): bool
    {
        return $this->getModel()->save();
    }

    public function select(): array
    {
        return $this->getModel()->select();
    }

    public function one(int $id): array
    {
        $model = &$this->getModel();

        return $model::find($id);
    }
    
    abstract public function getModel(): Model;
    
    public function getRedis(): Pool
    {
        throw new \ErrorException("请重写：".__METHOD__);
    }
}